<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Api\Controllers\Auths;

use App\Api\BaseApi;
use App\Models\GroupShipment;
use App\Models\PointShipment;
use App\Models\ProductShipment;
use App\Models\SalesApplication;

class ShipmentApi extends BaseApi
{
    public function productShipment() //商品发货单
    {
        $res = ProductShipment::with(['order' => function ($query) {
            $query->with(['product']);
        }])
            ->when(request('type', false), function ($query, $value) {
                $query->where('status', $value);
            })
            ->where('user_id', self::UserId())
            ->latest()
            ->simplePaginate(request('perPage', 10));
        return responses($res);
    }

    public function productShipmentDetails() //商品发货单详情
    {
        $res = ProductShipment::with(['order' => function ($query) {
            $query->with(['product']);
        }])
            ->where('user_id', self::UserId())
            ->find(request('id', 0));
        return responses($res);
    }

    public function confirmProductShipment() //确认商品发货单收货
    {
        $res = ProductShipment::query()
            ->where('user_id', self::UserId())
            ->find(request('id', 0));
        if ($res) {
            $res->status = 2;
            $res->save();
            return responses('确认成功');
        }
        return responses('无效的发货单', 7);
    }

    public function groupShipment() //拼团发货单
    {
        $res = GroupShipment::with(['order' => function ($query) {
            $query->with(['product']);
        }])
            ->when(request('type', false), function ($query, $value) {
                $query->where('status', $value);
            })
            ->where('user_id', self::UserId())
            ->latest()
            ->simplePaginate(request('perPage', 10));
        return responses($res);
    }

    public function groupShipmentDetails() //商品发货单详情
    {
        $res = GroupShipment::with(['order' => function ($query) {
            $query->with(['product']);
        }])
            ->where('user_id', self::UserId())
            ->find(request('id', 0));
        return responses($res);
    }

    public function confirmGroupShipment() //确认拼团发货单收货
    {
        $res = GroupShipment::query()
            ->where('user_id', self::UserId())
            ->find(request('id', 0));
        if ($res) {
            $res->status = 2;
            $res->save();
            return responses('确认成功');
        }
        return responses('无效的发货单', 7);
    }

    public function pointShipment() //积分商品发货单
    {
        $res = PointShipment::with(['order' => function ($query) {
            $query->with(['product']);
        }])
            ->when(request('type', false), function ($query, $value) {
                $query->where('status', $value);
            })
            ->where('user_id', self::UserId())
            ->latest()
            ->simplePaginate(request('perPage', 10));
        return responses($res);
    }

    public function pointShipmentDetails() //积分商品发货单
    {
        $res = PointShipment::with(['order' => function ($query) {
            $query->with(['product']);
        }])
            ->where('user_id', self::UserId())
            ->find(request('id', 0));
        return responses($res);
    }

    public function confirmPointShipment() //确认积分商品发货单收货
    {
        $res = PointShipment::query()
            ->where('user_id', self::UserId())
            ->find(request('id', 0));
        if ($res) {
            $res->status = 2;
            $res->save();
            return responses('确认成功');
        }
        return responses('无效的发货单', 7);
    }

    public function salesApplication() //售后申请
    {
        $request = self::validate([
            'order_number' => 'required',
            'type' => 'required|in:1,2,3',
        ]);

        SalesApplication::query()->create([
            'order_number' => $request->get('order_number'),
            'user_id' => self::UserId(),
            'attachment' => $request->get('attachment'),
            'leave_message' => $request->get('leave_message'),
            'type' => $request->get('type'),
            'status' => $request->get('status', 1),
        ]);
        return responses('售后以提交请耐心等待');
    }

    public function salesApplicationItem() //售后申请
    {
        $res = SalesApplication::query()
            ->where('user_id', self::UserId())
            ->simplePaginate(request('perPage', 10));
        return responses($res);
    }
}
